<?php

class TrainerFollowController extends GxController {

    public function actionfollowTrainner() {
        $userid = getLoginUserId();
        if (!empty($userid)) {
            $followers = getEmailbyuserId('Users', $userid);
            if (isset($_POST['type']) && $_POST['type'] == 'course') {
                $classDetails = Courses::model()->findByPk($_POST['class_id']);
                $profiles = getUserTypeByClassCourse('Courses', $_POST['class_id']);
                $trainername = $profiles->first_name . ' ' . $profiles->last_name;
            } else {
                $classDetails = Classes::model()->findByPk($_POST['class_id']);
                $profiles = getUserTypeByClassCourse('Classes', $_POST['class_id']);
                $trainername = $profiles->first_name . ' ' . $profiles->last_name;
            }
            $model = new TrainerFollow();
            if (!empty($classDetails->backend_user_id)) {
                $classUserId = '';
                $mailTousers = getEmailbyuserId('BackendUser', $classDetails->backend_user_id);
                $mailTouser = $mailTousers->email;
                $model->setAttribute('trainer_backend_user_id', $classDetails->backend_user_id);
            } else {
                $mailTousers = getEmailbyuserId('Users', $classDetails->users_id);
                $mailTouser = $mailTousers->username;
                $classUserId = $classDetails->users_id;
                $model->setAttribute('trainer_front_end_user_id', $classDetails->users_id);
            }
            $model->setAttribute('folower_user_id', $userid);
            $model->setAttribute('create_time', SkillConfig::CreateDateTime());
            $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
            $criteria = new CDbCriteria;
            $criteria->condition = 'folower_user_id=' . $userid . ' AND (trainer_front_end_user_id=' . $mailTousers->id . '  OR trainer_backend_user_id=' . $mailTousers->id . ')';
            $alredayFolow = TrainerFollow::model()->find($criteria);
            if (!empty($alredayFolow) && $alredayFolow->folower_user_id) {
                echo 'againsameuser';
            } else {
                if ($userid != $classUserId) {
                    $model->save();
                    $mailmodel = EmailTemplate::model()->findByPk(Message::NEW_FOLLOWER);
                    $userText = $mailmodel->body;
                    $mailbody = array('value' => $userText, 'type' => 'raw');                   
                    $userName = $trainername;
                    $followername = $followers->username;
                    $mailto = $mailTouser;
                    $arrayFind = array('[username]', '[followername]');
                    $arrayReplace = array($userName, $followername);
                    $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
                    $mailtemp = Template::model()->findByPk($mailmodel->template_id);
                    $subject = $mailmodel->subject;
                    $body = $this->renderPartial('/mails/' . $mailtemp->filename, array('mailbody' => $mailbody['value']), true, false);
                   try {
                        SkillSendMail::SendMail($mailto, $subject, $body);
                        echo CJSON::encode(array(
                            'status' => 'Success'
                        ));
                        Yii::app()->end();
                    } catch (Exception $e) {
                        $result = $e;
                        exit;
                    }
                } else {
                    echo 'Fail';
                }
            }
        } else {
            echo 'redirect';
        }
    }

}